package com.steel.yfmall.cloud.assistant.account.impl;

import com.alibaba.fastjson.JSON;
import com.steel.yfmall.cloud.assistant.account.AccountBaseService;
import com.steel.yfmall.cloud.assistant.dto.para.account.ParaAcountBaseBean;
import com.steel.yfmall.cloud.assistant.dto.para.account.ParaCloseAccountInfoBean;
import com.steel.yfmall.cloud.assistant.dto.para.account.ParaERPUserInfoBean;
import com.steel.yfmall.cloud.assistant.po.user.UserAccounts;
import com.steel.yfmall.cloud.assistant.po.user.UserAccountsExample;
import com.steel.yfmall.cloud.assistant.po.user.UserBase;
import com.steel.yfmall.cloud.assistant.po.user.UserBaseExample;
import com.steel.yfmall.cloud.assistant.user.mapper.UserAccountsMapper;
import com.steel.yfmall.cloud.assistant.user.mapper.UserBaseMapper;
import com.steel.yfmall.cloud.components.mq.service.MqSendService;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AccountBaseServiceImpl implements AccountBaseService  {

    @Override
    public APIMsgBean<Boolean> accountSyncInfo(ParaAcountBaseBean paraAcountBaseBean) {
        APIMsgBean<Boolean> booleanAPIMsgBean = new APIMsgBean<>();
        if (paraAcountBaseBean == null || paraAcountBaseBean.getAccount() == null || paraAcountBaseBean.getAccount().size() == 0) {
            booleanAPIMsgBean.setData(false);
            booleanAPIMsgBean.getSoaStateBean().setMessage("参数异常，状态不能为空");
            return booleanAPIMsgBean;
        }
        //苍穹传来的list
        List<ParaAcountBaseBean.AccountInfo> list = paraAcountBaseBean.getAccount();
        String username=list.get(0).getUserName();
        if (StringUtils.isBlank(username)) {
            booleanAPIMsgBean.setData(false);
            booleanAPIMsgBean.getSoaStateBean().setMessage("用户名不能为空");
            return booleanAPIMsgBean;
        }

        try {

            for (ParaAcountBaseBean.AccountInfo accountInfo : list) {
                UserAccountsExample example = new UserAccountsExample();
                example.createCriteria().andUserNameEqualTo(accountInfo.getUserName()).andAccountNumberEqualTo(accountInfo.getAccountNumber()).andCompanyNameEqualTo(accountInfo.getCompanyName());
                UserAccounts userAccounts = userAccountsMapper.selectOneByExampleSelective(example);
                if (userAccounts != null) {
                    //status= 2直接物理删除
                    if (accountInfo.getStatus() == 2) {
                        example = new UserAccountsExample();
                        example.createCriteria().andIdEqualTo(userAccounts.getId());
                        userAccountsMapper.deleteByExample(example);
                    } else {
                        UserAccounts record = new UserAccounts();
                        record.setStatus(accountInfo.getStatus());
                        example = new UserAccountsExample();
                        example.createCriteria().andIdEqualTo(userAccounts.getId());
                        userAccountsMapper.updateByExampleSelective(record,example);
                    }
                } else {
                    userAccounts = new UserAccounts();
                    userAccounts.setUserName(accountInfo.getUserName());
                    userAccounts.setAccountNumber(accountInfo.getAccountNumber());
                    userAccounts.setAccountType(0);
                    userAccounts.setCompanyName(accountInfo.getCompanyName());
                    userAccounts.setProductTypeNo("");
                    userAccounts.setStatus(accountInfo.getStatus());
                    userAccounts.setCreateTime(new Date());
                    userAccounts.setCreateUser("苍穹");
                    userAccounts.setUpdateUser("苍穹");
                    userAccounts.setUpdateTime(new Date());
                    userAccountsMapper.insertSelective(userAccounts);
                }
            }
//
//            booleanAPIMsgBean.setData(true);
//            booleanAPIMsgBean.getSoaStateBean().setMessage("保存成功！");
//            OutCallApiLogBean outCallApiLogBean=new OutCallApiLogBean();
//            outCallApiLogBean.setApiUrl("/userAcountInfo");
//            outCallApiLogBean.setParaText(paraAcountBaseBean.toString());
//            outCallApiLogBean.setInterfaceMethod("accountSyncInfo");
//            outCallApiLogBean.setInterfaceMethodName("账号修改和新增");
//            RequestSendMQBean requestSendMQBean = new RequestSendMQBean();
//            requestSendMQBean.setSendMqTitle("insert_out_call_api_log");
//            requestSendMQBean.setSendMqMessage(JSON.toJSONString(outCallApiLogBean));
//            mqSendService.sendCommonMQMessage(requestSendMQBean);

        } catch (Exception ex) {
            ex.printStackTrace();
            log.info("账号修改和新增异常");
        }
        booleanAPIMsgBean.getSoaStateBean().setCode(1);
        booleanAPIMsgBean.getSoaStateBean().setMessage("成功");
        booleanAPIMsgBean.setData(true);
        return booleanAPIMsgBean;
    }

    @Override
    public APIMsgBean<Boolean> updateERPUserName(ParaERPUserInfoBean paraERPUserInfoBean) {
        APIMsgBean<Boolean> apiMsgBean = new APIMsgBean<>();
        apiMsgBean.getSoaStateBean().setCode(0);
        apiMsgBean.getSoaStateBean().setMessage("参数异常，无法修改！");
        if (paraERPUserInfoBean == null || StringUtils.isBlank(paraERPUserInfoBean.getCustNo()) || StringUtils.isBlank(paraERPUserInfoBean.getErpCustNo())) {
            return apiMsgBean;
        }
        UserBaseExample example = new UserBaseExample();
        example.createCriteria().andUserNameEqualTo(paraERPUserInfoBean.getCustNo());
        List<UserBase> userBases = userBaseMapper.selectByExample(example);

        if (userBases == null || userBases.size() == 0) {
            return apiMsgBean;
        }
        UserBase record = new UserBase();
        record.setOtherUserCode(paraERPUserInfoBean.getErpCustNo());

        UserBaseExample userBaseExample = new UserBaseExample();
        userBaseExample.createCriteria().andUserNameEqualTo(paraERPUserInfoBean.getCustNo());
        int i = userBaseMapper.updateByExampleSelective(record, userBaseExample);
        if (i > 0) {
            apiMsgBean.getSoaStateBean().setCode(1);
            apiMsgBean.getSoaStateBean().setMessage("修改成功");
            return apiMsgBean;
        }
        return apiMsgBean;
    }

    @Override
    public APIMsgBean<Boolean> closeAccountInfo(ParaCloseAccountInfoBean paraCloseAccountInfoBean) {
        log.info("关闭子账户接口closeAccountInfo，参数"+ JSON.toJSONString(paraCloseAccountInfoBean));
        APIMsgBean<Boolean> apiMsgBean = new APIMsgBean<>();
        apiMsgBean.getSoaStateBean().setMessage("参数异常，无法修改！");
        if (paraCloseAccountInfoBean == null || StringUtils.isBlank(paraCloseAccountInfoBean.getUserName()) || paraCloseAccountInfoBean.getStatus() == null) {
            return apiMsgBean;
        }
        //构造查询 m_user_base表实体
        UserBaseExample userBaseExample = new UserBaseExample();
        //创建一下查询条件，userName：云苍穹带参数请求
        userBaseExample.createCriteria().andUserNameEqualTo(paraCloseAccountInfoBean.getUserName());
        //根据请求参数的值，进行查询
        UserBase userBase = userBaseMapper.selectOneByExampleSelective(userBaseExample);
        //'用户状态  -1为删除，
        if (paraCloseAccountInfoBean.getStatus() == -1 ) {
            if (userBase != null) {
                userBaseMapper.deleteByPrimaryKey(userBase.getId());
            }
            apiMsgBean.setData(true);
            apiMsgBean.getSoaStateBean().setCode(1);
            apiMsgBean.getSoaStateBean().setMessage("删除成功");
            return apiMsgBean;
        }
        //判断userBases 是否为空
        if (userBase == null) {
            apiMsgBean.getSoaStateBean().setMessage("电商不存在此账号，无法操作！");
            return apiMsgBean;
        }
        UserBase userBaseOther = new UserBase();
        //获取云苍穹传过来的status的值
        userBaseOther.setStatus(paraCloseAccountInfoBean.getStatus());
        userBaseOther.setUpdateTime(new Date());
        //'用户状态  -1为删除，0为未审核，1为已审核，2为已激活',3为关闭
        if (userBase.getStatus().equals(0) || userBase.getStatus().equals(1) || userBase.getStatus().equals(2) || userBase.getStatus().equals(3)) {
            int i = userBaseMapper.updateByExampleSelective(userBase, userBaseExample);
            if (i > 0) {
                apiMsgBean.getSoaStateBean().setCode(1);
                apiMsgBean.getSoaStateBean().setMessage("成功");
                return apiMsgBean;
            }
        }
        return apiMsgBean;
    }
    @Resource
    private UserBaseMapper userBaseMapper;
    @Resource
    private UserAccountsMapper userAccountsMapper;
    @Resource
    private MqSendService mqSendService;
}
